package com.softer.wxzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softer.wxzj.common.entity.CommonResult;
import com.softer.wxzj.common.entity.Page;
import com.softer.wxzj.common.enums.NumStrEnum;
import com.softer.wxzj.common.enums.NumberEnum;
import com.softer.wxzj.common.enums.ShiroResultEnum;
import com.softer.wxzj.common.error.CourseException;
import com.softer.wxzj.common.flow.enums.FlowStateEnum;
import com.softer.wxzj.common.flow.enums.FlowTypeEnum;
import com.softer.wxzj.common.flow.util.FlowAuditUtil;
import com.softer.wxzj.common.flow.util.FlowUpdateAuditUtil;
import com.softer.wxzj.common.flow.util.Result;
import com.softer.wxzj.common.util.*;
import com.softer.wxzj.entity.*;
import com.softer.wxzj.mapper.*;
import com.softer.wxzj.service.BaseHouseInfoService;
import com.softer.wxzj.service.BaseModifyReqService;
import com.softer.wxzj.service.ZoneCodeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@Service
@Transactional(rollbackFor = {RuntimeException.class, Exception.class})
public class BaseModifyReqServiceImpl implements BaseModifyReqService {
    @Autowired
    private BaseModifyReqMapper baseModifyReqMapper;
    @Autowired
    private FlowAuditUtil flowUtil;
    @Autowired
    private OperUtil operUtil;
    @Autowired
    private ZoneCodeService zoneCodeService;
    @Autowired
    private Result result;
    @Autowired
    private FlowUpdateAuditUtil flowUpdateAuditUtil;
    @Autowired
    private ModifyUtil<BaseCellInfo> modifyUtil;
    @Autowired
    private BaseHouseInfoMapper baseHouseInfoMapper;
    @Autowired
    private BaseModifyDetailMapper modifyDetailMapper;
    @Autowired
    private BaseOwnerInfoMapper baseOwnerInfoMapper;
    @Autowired
    private BaseCellInfoMapper baseCellInfoMapper;
    @Autowired
    private BaseModifyDetailMapper baseModifyDetailMapper;
    @Autowired
    private FlowInstanceMapper flowInstanceMapper;
    @Autowired
    private DicContentMapper dicContentMapper;
    @Autowired
    private BaseHouseInfoService baseHouseInfoService;

    @Override
    public CommonResult update(BaseModifyReq baseModifyReq) {
        return null;
    }

    @Override
    public String userName() {
        return null;
    }

    @Override
    public CommonResult insert(BaseModifyReq b) {
        int i = baseModifyReqMapper.insert(b);
        return null;
    }

    /**
     * 变更审核列表审核
     *
     * @param page
     * @param baseModifyReq
     * @return
     */
    @Override
    public PageInfo<BaseModifyReq> getAuditUpdateList(Page page, BaseModifyReq baseModifyReq) {
        //查询业务
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo("committee_update");
        List<BaseModifyReq> list = new ArrayList<>();
        if (null != map) {
            //调用公用方法
            baseModifyReq.setId(map.get("id"));
            // 用户绑定的区域
            String regionCode = map.get("zoneCode");
            baseModifyReq.setRegionCode(regionCode);
            baseModifyReq.setFlowState(NumStrEnum.ONE.getNum());
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            list = baseModifyReqMapper.getAuditUpdateList(baseModifyReq);
        }
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 变更查询表查询
     *
     * @param page
     * @param baseModifyReq
     * @return
     */
    @Override
    public PageInfo<BaseModifyReq> getList(Page page, BaseModifyReq baseModifyReq) {

        //赋值区域,重要
        baseModifyReq.setZoneCode(operUtil.getZoneCode());
        //模糊查询赋值
        setFuzzyParam(baseModifyReq);
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseModifyReq> list = baseModifyReqMapper.getList(baseModifyReq);
        for (BaseModifyReq q : list) {
            //设置要获取到什么样的时间
            Date date1 = null;
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            try {
                date1 = sdf.parse(q.getCreateTime());
            } catch (ParseException e) {
                log.error("ParseException错误信息:()", e.getMessage(), e);
            }
            String datestr = sdf.format(date1);
            q.setCreateTime(datestr);
        }
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 模糊参数转换赋值
     *
     * @param baseModifyReq
     */
    private void setFuzzyParam(BaseModifyReq baseModifyReq) {
        if (null != baseModifyReq && com.softer.wxzj.common.util.StringUtils.isNotBlank(baseModifyReq.getFuzzyParam())) {
            baseModifyReq.setFuzzyParam(FuzzyConversionUtil.getFlowState(baseModifyReq.getFuzzyParam()));
        }
    }


    /**
     * 查询业主信息变更列表
     *
     * @param page
     * @param baseModifyReq
     * @return
     */
    @Override
    public PageInfo<BaseModifyReq> getChangeOwnerList(Page page, BaseModifyReq baseModifyReq) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        //获取list 后根据小区的区域编码 获取小区的详细信息
        List<BaseModifyReq> list = baseModifyReqMapper.getChangeOwnerList(baseModifyReq);
        for (int i = 0; i < list.size(); i++) {
            if (StringUtils.isBlank(list.get(i).getZoneCode())) {
                list.get(i).setName(zoneCodeService.zoneCodeSuperiors(list.get(i).getZoneCode(), ShiroUserUtil.getUserName()) + '/' + list.get(i).getName());
            }
        }
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 查询房屋信息变更列表
     *
     * @param page
     * @param baseModifyReq
     * @return
     */
    @Override
    public PageInfo<BaseModifyReq> selectUpdateHouseList(Page page, BaseModifyReq baseModifyReq) {
        //赋值区域,重要
        baseModifyReq.setRegionCode(operUtil.getZoneCode());
        //获取list 后根据小区的区域编码 获取小区的详细信息
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        baseModifyReq.setFlowState("1,2,3,4");
        List<BaseModifyReq> list = baseModifyReqMapper.selectUpdate(baseModifyReq);
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 查询房间信息变更审核列表
     *
     * @param page
     * @param baseModifyReq
     * @return
     */
    @Override
    public PageInfo<BaseModifyReq> selectUpdate(Page page, BaseModifyReq baseModifyReq) {
        //查询业务
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(FlowTypeEnum.BASE_HOUSE_INFO_UPDATE.getType());
        List<BaseModifyReq> list = new ArrayList<>();
        if (null != map) {

            //调用公用方法
            // 从审批流提取的业务id集合
            baseModifyReq.setBusinessId(map.get("id"));
            // 用户绑定的区域
            String regionCode = map.get("zoneCode");
            baseModifyReq.setRegionCode(regionCode);
            baseModifyReq.setFlowState(NumStrEnum.ONE.getNum());
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            list = baseModifyReqMapper.selectUpdate(baseModifyReq);
        }
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 自然幢变更审核列表
     *
     * @param page
     * @param baseModifyReq
     * @param flowType
     * @return
     */
    @Override
    public PageInfo<BaseModifyReq> getUpdateList(Page page, BaseModifyReq baseModifyReq, String flowType) {
        //查询业务
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(flowType);
        List<BaseModifyReq> list = new ArrayList<>();
        if (null != map) {
            //调用公用方法
            // 从审批流提取的业务id集合
            baseModifyReq.setBusinessId(map.get("id"));
            // 用户绑定的区域
            baseModifyReq.setZoneCode(map.get("zoneCode"));
            baseModifyReq.setModifyType("base_build_info_update");
            //业务表的审核状态 1审核中
            baseModifyReq.setFlowState(String.valueOf(NumberEnum.ONE.getNum()));
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            list = baseModifyReqMapper.selectChange(baseModifyReq);
        }
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 自然幢变更列表
     *
     * @param page
     * @param baseModifyReq
     * @return
     */
    @Override
    public PageInfo<BaseModifyReq> getUpdate(Page page, BaseModifyReq baseModifyReq) {
        baseModifyReq.setZoneCode(operUtil.getZoneCode());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseModifyReq> list = baseModifyReqMapper.selectChange(baseModifyReq);
        for (BaseModifyReq b : list) {
            if (StringUtils.isNotBlank(b.getSubRegionName())) {
                b.setBuildName(b.getBuildName() + "(" + b.getSubRegionName() + ")");
            }
        }
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 模糊查询业主信息变更列表
     *
     * @param page  分页实体
     * @param param 模糊查询参数
     * @return 业主信息变更列表
     */
    @Override
    public PageInfo<BaseOwnerFuzzy> ownerModifyFuzzySearch(Page page, String param) {
        String zoneCode = operUtil.getZoneCode();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseOwnerFuzzy> list = baseModifyReqMapper.ownerModifyFuzzySearch(zoneCode, param);
        PageInfo<BaseOwnerFuzzy> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 模糊查询小区变更审核列表
     *
     * @param page  分页实体
     * @param param 模糊查询参数
     * @return 小区变更审核列表
     */
    @Override
    public PageInfo<BaseRegionInfo> regionModifyFuzzySearch(Page page, String param) {
        //查询业务
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(FlowTypeEnum.REGION_INFO_MODIFY.getType());
        BaseRegionInfo baseRegionInfo = new BaseRegionInfo();
        List<BaseRegionInfo> list = new ArrayList<>();
        if (map != null) {
            result.selectParm(map, baseRegionInfo);
            baseRegionInfo.setFuzzyParam(param);
            baseRegionInfo.setModifyType(FlowTypeEnum.REGION_INFO_MODIFY.getType());
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            list = baseModifyReqMapper.regionModifyFuzzySearch(baseRegionInfo);
        }
        PageInfo<BaseRegionInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 模糊查询自然幢变更审核列表
     *
     * @param page  分页实体
     * @param param 模糊查询参数
     * @return 自然幢变更审核列表
     */
    @Override
    public PageInfo<BaseBuildInfo> buildModifyFuzzySearch(Page page, String param) {
        //查询业务
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(FlowTypeEnum.BASE_BUILD_INFO_UPDATE.getType());
        BaseBuildInfo baseBuildInfo = new BaseBuildInfo();
        List<BaseBuildInfo> list = new ArrayList<>();
        if (map != null) {
            // 从审批流提取的业务id集合
            baseBuildInfo.setBusinessId(map.get("id"));
            // 用户绑定的区域
            baseBuildInfo.setZoneCode(map.get("zoneCode"));
            baseBuildInfo.setFuzzyParam(param);
            baseBuildInfo.setModifyType(FlowTypeEnum.BASE_BUILD_INFO_UPDATE.getType());
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            list = baseModifyReqMapper.buildModifyFuzzySearch(baseBuildInfo);
        }
        PageInfo<BaseBuildInfo> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 模糊查询业主变更审核列表
     *
     * @param page  分页实体
     * @param param 模糊查询参数
     * @return 业主变更审核列表
     */
    @Override
    public PageInfo<BaseOwnerFuzzy> ownerModifyAuditFuzzySearch(Page page, String param) {
        //查询业务
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(FlowTypeEnum.BASE_OWNER_INFO_UPDATE.getType());
        BaseOwnerFuzzy baseOwnerFuzzy = new BaseOwnerFuzzy();
        List<BaseOwnerFuzzy> list = new ArrayList<>();
        if (map != null) {
            result.selectParm(map, baseOwnerFuzzy);
            baseOwnerFuzzy.setFuzzyParam(param);
            baseOwnerFuzzy.setModifyType(FlowTypeEnum.BASE_OWNER_INFO_UPDATE.getType());
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            list = baseModifyReqMapper.ownerModifyAuditFuzzySearch(baseOwnerFuzzy);
        }
        PageInfo<BaseOwnerFuzzy> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 模糊查询房屋调换审核列表
     *
     * @param page  分页实体
     * @param param 模糊查询参数
     * @return 房屋调换审核列表
     */
    @Override
    public PageInfo<BaseBuildModifyDetail> houseModifyAuditFuzzySearch(Page page, String param) {
        //查询业务
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(FlowTypeEnum.HOUSE_MODIFY.getType());
        BaseBuildModifyDetail baseBuildModifyDetail = new BaseBuildModifyDetail();
        List<BaseBuildModifyDetail> list = new ArrayList<>();
        if (map != null) {
            baseBuildModifyDetail.setId(map.get("id"));
            //赋值区域,重要
            baseBuildModifyDetail.setZoneCode(map.get("zoneCode"));
            baseBuildModifyDetail.setFuzzyParam(param);
            baseBuildModifyDetail.setModifyType(FlowTypeEnum.HOUSE_MODIFY.getType());
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            list = baseModifyReqMapper.houseModifyAuditFuzzySearch(baseBuildModifyDetail);
        }
        PageInfo<BaseBuildModifyDetail> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 模糊查询房屋合并/拆分审核列表
     *
     * @param page  分页实体
     * @param param 模糊查询参数
     * @return 房屋合并/拆分审核列表
     */
    @Override
    public PageInfo<SplitMergeReqList> splitMergeAuditFuzzySearch(Page page, String param) {
        //查询业务
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(FlowTypeEnum.SPLIT_MERGE.getType());
        SplitMergeReqList splitMergeReqList = new SplitMergeReqList();
        List<SplitMergeReqList> list = new ArrayList<>();
        if (map != null) {
            splitMergeReqList.setId(map.get("id"));
            splitMergeReqList.setState(NumberEnum.ONE.getNum());
            splitMergeReqList.setFuzzyParam(param);
            //赋值区域,重要
            splitMergeReqList.setZoneCode(operUtil.getZoneCode());
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            list = baseModifyReqMapper.splitMergeAuditFuzzySearch(splitMergeReqList);
        }
        PageInfo<SplitMergeReqList> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 模糊查询房屋变更审核列表
     *
     * @param page  分页实体
     * @param param 模糊查询参数
     * @return 房屋变更审核列表
     */
    @Override
    public PageInfo<BaseModifyReq> houseChangeAuditFuzzySearch(Page page, String param) {
        //查询业务
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(FlowTypeEnum.BASE_HOUSE_INFO_UPDATE.getType());
        List<BaseModifyReq> list = new ArrayList<>();
        if (null != map) {
            BaseHouseInfo baseHouseInfo = new BaseHouseInfo();
            //调用公用方法
            // 从审批流提取的业务id集合
            baseHouseInfo.setBusinessId(map.get("id"));
            baseHouseInfo.setFuzzyParam(param);
            // 用户绑定的区域
            String regionCode = map.get("zoneCode");
            baseHouseInfo.setLoginZoneCode(regionCode);
            baseHouseInfo.setState(NumberEnum.ONE.getNum() + "");
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            list = baseModifyReqMapper.houseChangeAuditFuzzySearch(baseHouseInfo);
        }
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 模糊查询小区变更查询列表
     *
     * @param page       分页实体
     * @param param      模糊查询参数
     * @param businessId 小区ID
     * @return 小区变更查询列表
     */
    @Override
    public PageInfo<BaseModifyReq> regionAuditSearchFuzzy(Page page, String param, String businessId) {
        String zoneCode = operUtil.getZoneCode();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseModifyReq> list = baseModifyReqMapper.regionAuditSearchFuzzy(zoneCode, param, businessId);
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 模糊查询自然幢变更查询列表
     *
     * @param page  分页实体
     * @param param 模糊查询参数
     * @return 自然幢变更查询列表
     */
    @Override
    public PageInfo<BaseModifyReq> buildAuditSearchFuzzy(Page page, String param) {
        String zoneCode = operUtil.getZoneCode();
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseModifyReq> list = baseModifyReqMapper.buildAuditSearchFuzzy(zoneCode, param);
        for (BaseModifyReq b : list) {
            if (StringUtils.isNotBlank(b.getSubRegionName())) {
                b.setBuildName(b.getBuildName() + "(" + b.getSubRegionName() + ")");
            }
        }
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 模糊查询房屋调换变更查询列表
     *
     * @param page  分页实体
     * @param param 模糊查询参数
     * @return 房屋调换变更查询列表
     */
    @Override
    public PageInfo<BaseBuildModifyDetail> houseModifyAuditSearchFuzzy(Page page, String param) {
        BaseBuildModifyDetail baseBuildModifyDetail = new BaseBuildModifyDetail();
        baseBuildModifyDetail.setModifyType(FlowTypeEnum.HOUSE_MODIFY.getType());
        baseBuildModifyDetail.setFuzzyParam(param);
        //赋值区域,重要
        baseBuildModifyDetail.setZoneCode(operUtil.getZoneCode());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseBuildModifyDetail> list = baseModifyReqMapper.houseModifyAuditSearchFuzzy(baseBuildModifyDetail);
        PageInfo<BaseBuildModifyDetail> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 模糊查询房屋变更查询列表
     *
     * @param page  分页实体
     * @param param 模糊查询参数
     * @return 房屋变更查询列表
     */
    @Override
    public PageInfo<BaseModifyReq> houseChangeAuditSearchFuzzy(Page page, String param) {
        BaseHouseInfo baseHouseInfo = new BaseHouseInfo();
        baseHouseInfo.setFuzzyParam(param);
        //赋值区域,重要
        baseHouseInfo.setLoginZoneCode(operUtil.getZoneCode());
        baseHouseInfo.setModifyType(FlowTypeEnum.BASE_HOUSE_INFO_UPDATE.getType());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseModifyReq> list = baseModifyReqMapper.houseChangeAuditSearchFuzzy(baseHouseInfo);
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 模糊查询房屋变更查询列表
     *
     * @param page          分页实体
     * @param baseHouseInfo 模糊查询参数
     * @return 房屋变更查询列表
     */
    @Override
    public PageInfo<BaseModifyReq> houseChangeAuditSearchFuzzyByHouse(Page page, BaseHouseInfo baseHouseInfo) {
        //赋值区域,重要
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseModifyReq> list = baseModifyReqMapper.houseChangeAuditSearchFuzzyByHouse(baseHouseInfo);
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 模糊查询房屋业主变更查询列表
     *
     * @param page  分页实体
     * @param param 模糊查询参数
     * @return 房屋业主变更查询列表
     */
    @Override
    public PageInfo<BaseModifyReq> ownerAuditSearchFuzzy(Page page, String param) {
        BaseOwnerFuzzy baseOwnerFuzzy = new BaseOwnerFuzzy();
        baseOwnerFuzzy.setFuzzyParam(param);
        //赋值区域,重要
        baseOwnerFuzzy.setLoginZoneCode(operUtil.getZoneCode());
        baseOwnerFuzzy.setModifyType(FlowTypeEnum.BASE_OWNER_INFO_UPDATE.getType());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseModifyReq> list = baseModifyReqMapper.ownerAuditSearchFuzzy(baseOwnerFuzzy);
        for (BaseModifyReq l : list) {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date date = null;
            try {
                date = sdf.parse(l.getCreateTime());
            } catch (ParseException e) {
                log.error("错误信息:()", e.getMessage(), e);
            }
            l.setCreateTime(sdf.format(date));
        }
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 业主变更查询列表
     *
     * @param baseModifyReq
     * @param page
     * @return
     */
    @Override
    public PageInfo<BaseModifyReq> selectChangeOwner(BaseModifyReq baseModifyReq, Page page) {
        baseModifyReq.setZoneCode(operUtil.getZoneCode());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseModifyReq> list = baseModifyReqMapper.selectChangeOwner(baseModifyReq);
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 业主变更审核列表
     *
     * @param baseModifyReq
     * @param page
     * @return
     */
    @Override
    public PageInfo<BaseModifyReq> getOwnerUpdateList(BaseModifyReq baseModifyReq, Page page) {
        //查询业务
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(FlowTypeEnum.BASE_OWNER_INFO_UPDATE.getType());
        List<BaseModifyReq> list = new ArrayList<>();
        if (null != map) {
            //调用公用方法
            // 从审批流提取的业务id集合
            baseModifyReq.setBusinessId(map.get("id"));
            // 用户绑定的区域
            String regionCode = map.get("zoneCode");
            baseModifyReq.setRegionCode(regionCode);
            baseModifyReq.setFlowState(NumStrEnum.ONE.getNum());
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            list = baseModifyReqMapper.getOwnerUpdateList(baseModifyReq);
        }
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 结构变更提交
     *
     * @param id
     * @param buildId
     * @return
     */
    @Override
    @Transactional
    public CommonResult addModify(String id, String buildId, String modifyCause) {
        //查询所有的修改的房屋的状态是否正常，若不正常则无法提交
        String flowType = flowInstanceMapper.getListByBusinessId(buildId);
        if (StringUtils.isNotBlank(flowType)) {
            return CommonResult.idResult(201, "该自然幢正在进行" + flowType + "业务，请稍后重试");
        }
        List<String> list = baseModifyDetailMapper.selectAllHouseId(id);
        for (String allIds : list) {
            BaseHouseInfo b = baseHouseInfoMapper.getInfo(allIds);
            if (null != b) {
                //房屋状态
                baseHouseInfoService.houseState(b);
            }
            }
        List<String> ids = modifyDetailMapper.selectAddHouse(id, NumStrEnum.ONE.getNum());
        for (String ida : ids) {
            int num = modifyDetailMapper.selectAdd(id, ida);
            if (num > 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), "房屋未全部维护，请维护完之后再提交");
            }
        }
        //绑定审批流
        int no = 0;
        String initState = flowUpdateAuditUtil.initFlow(buildId, id, "base_cell_info_update", null);
        no = baseModifyReqMapper.updateMsg(id, modifyCause);
        if (no == 0) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), "添加审核意见");
        }
        List<String> changeHouseState = baseModifyDetailMapper.selectDelHouseId(id);
        for (String hid : changeHouseState) {
            baseHouseInfoMapper.updateStateById(hid, NumStrEnum.THREE.getNum());
        }
        //当返回通过时表明该记录不需要审核，直接更新到业务表
        if (FlowStateEnum.PASS.getState().equals(initState)) {
            int change = baseModifyReqMapper.updateStateById(id, initState);
            if (change == 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), "修改变更表状态失败");
            }
            no = change(id, buildId);
        } else if (FlowStateEnum.VETO.getState().equals(initState)) {
            int change = baseModifyReqMapper.updateStateById(id, initState);
            if (change == 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), "修改变更表状态失败");
            }
            vertoHouse(id);
        }
        return CommonResult.ResponseResult(no);
    }

    private int vertoHouse(String id) {
        //若审核失败把房屋的状态改回来
        int no = 1;
        String ids = modifyDetailMapper.selectDelHouse(id, NumStrEnum.ONE.getNum());
        if (ids != null) {
            String[] idList = ids.split(",");
            for (String houseId : idList) {
                BaseOwnerInfo b = baseOwnerInfoMapper.getInfoByAccNo(houseId);
                if (b != null) {
                    no = baseHouseInfoMapper.updateStateById(houseId, NumStrEnum.ONE.getNum());
                } else {
                    no = baseHouseInfoMapper.updateStateById(houseId, NumStrEnum.ZERO.getNum());
                }
            }
        }

        return no;
    }

    /**
     * 结构变更审核
     *
     * @param buildNo
     * @param id
     * @param opinion 审核意见
     * @param mark
     * @return
     */
    @Override
    @Transactional
    public CommonResult changeResidentialCommittee(String buildNo, String id, String opinion, String mark) {
        int num = 0;
        String businessId = baseModifyReqMapper.getBusinessId(id);
        String auditState = flowUpdateAuditUtil.audit(businessId, opinion, mark);
        //终审
        if (null != auditState) {
            if (FlowStateEnum.PASS.getState().equals(auditState)) {
                //查询数据库原数据
                num = change(id, buildNo);
            } else {//若审核失败把房屋的状态改回来
                num = vertoHouse(id);
            }
        }
        return CommonResult.ResponseResult(num);
    }

    /**
     * 结构变更审核列表
     *
     * @param baseModifyReq
     * @param page
     * @return
     */
    @Override
    public PageInfo<BaseModifyReq> getCellUpdateList(BaseModifyReq baseModifyReq, Page page) {
        //查询业务
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(FlowTypeEnum.BASE_CELL_INFO_UPDATE.getType());
        List<BaseModifyReq> list = new ArrayList<>();
        if (null != map) {
            //调用公用方法
            // 从审批流提取的业务id集合
            baseModifyReq.setBusinessId(map.get("id"));
            // 用户绑定的区域
            String regionCode = map.get("zoneCode");
            baseModifyReq.setZoneCode(regionCode);
            baseModifyReq.setFlowState(NumStrEnum.ONE.getNum());
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            list = baseModifyReqMapper.getCellUpdateList(baseModifyReq);
        }
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 结构变更查询列表
     *
     * @param baseModifyReq
     * @param page
     * @return
     */
    @Override
    public PageInfo<BaseModifyReq> selectChangeCell(BaseModifyReq baseModifyReq, Page page) {
        baseModifyReq.setZoneCode(operUtil.getZoneCode());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseModifyReq> list = baseModifyReqMapper.selectChangeCell(baseModifyReq);
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 房屋结构变更提交审核
     *
     * @param id
     * @param modifyCause
     * @return
     */
    @Override
    public CommonResult addModifyCell(String id, String buildId, String modifyCause) {
        List<String> ids = Arrays.asList(id.split(","));
        int change = baseModifyDetailMapper.selectChangeHouseAre(id);
        if (change == 0 || StringUtils.isBlank(id)) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), "未选择单元或所选的单元未变更请重试");
        } else {
            for (String idss : ids) {
                change = baseModifyDetailMapper.selectChangeHouseAre(idss);
                if (change == 0) {
                    BaseCellInfo base = baseCellInfoMapper.getInfo(idss);
                    String name = dicContentMapper.selectName(base.getCellType());
                    throw new CourseException(ShiroResultEnum.FAIL.getCode(), base.getCellName() + "(" + name + ")" + "单元未变更");
                }
            }
        }
        String type = flowInstanceMapper.getListByBusinessId(buildId);
        if (StringUtils.isNotBlank(type)) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), "该自然幢正在进行" + type + "业务，无法提交");
        }
        //查询删除数据
        List<String> list = baseModifyDetailMapper.selectDelHouseByReqNo(id);
        for (String delId : list) {
            baseHouseInfoMapper.updateStateById(delId, NumStrEnum.THREE.getNum());
        }
        //查询修改数据
        List<String> changeIds = baseModifyDetailMapper.selectBusinessNextByReqNoIdNull(id);
        List<String> changeHouseInfo = modifyDetailMapper.reqNoChangeHouse(null);
        for (String chang : changeHouseInfo) {
            changeIds.add(chang);
        }
        for (String changeId : changeIds) {
            int changeHouse = baseModifyDetailMapper.selectChangeHouseById(id, changeId);
            if (changeHouse == 0) {
                baseHouseInfoMapper.updateStateById(changeId, NumStrEnum.THREE.getNum());
            }
        }
        String uuid = UUIDUtil.getUUID();
        //绑定审批流
        int no = 0;
        BaseModifyReq baseModifyReq = new BaseModifyReq();
        baseModifyReq.setId(uuid);
        baseModifyReq.setBusinessId(buildId);
        baseModifyReq.setCreater(ShiroUserUtil.getUserName());
        baseModifyReq.setModifyType("base_house_info_structure");
        baseModifyReq.setModifyCause(modifyCause);
        no = baseModifyReqMapper.insert(baseModifyReq);
        if (no == 0) {
            throw new CourseException(ShiroResultEnum.FAIL.getCode(), "添加审核意见");
        }
        no = modifyDetailMapper.updateReqNoByBusinessId(uuid, id);

        String initState = flowUpdateAuditUtil.initFlow(buildId, uuid, "base_house_info_structure", null);
        //当返回通过时表明该记录不需要审核，直接更新到业务表
        if (FlowStateEnum.PASS.getState().equals(initState)) {
            no = changeHouse(uuid, buildId, id);
        }
        return CommonResult.ResponseResult(no);
    }

    @Override
    public PageInfo<BaseModifyReq> getHouseUpdateList(BaseModifyReq baseModifyReq, Page page) {

        //查询业务
        Map<String, String> map = flowUtil.getBussIdAndZoneCodeByOperNo(FlowTypeEnum.BASE_HOUSE_INFO_STRUCTURE.getType());
        List<BaseModifyReq> list = new ArrayList<>();
        if (null != map) {
            //调用公用方法
            // 从审批流提取的业务id集合
            baseModifyReq.setBusinessId(map.get("id"));
            // 用户绑定的区域
            String regionCode = map.get("zoneCode");
            baseModifyReq.setZoneCode(regionCode);
            baseModifyReq.setFlowState(NumStrEnum.ONE.getNum());
            PageHelper.startPage(page.getPageNum(), page.getPageSize());
            list = baseModifyReqMapper.getHouseUpdateList(baseModifyReq);
        }
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public PageInfo<BaseModifyReq> selectChangeHouse(BaseModifyReq baseModifyReq, Page page) {
        baseModifyReq.setZoneCode(operUtil.getZoneCode());
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        List<BaseModifyReq> list = baseModifyReqMapper.selectChangeHouse(baseModifyReq);
        PageInfo<BaseModifyReq> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    @Transactional
    public CommonResult resetting(String id, String type) {
        int num = 0;
        if (("base_cell_info_update").equals(type)) {
            num = baseModifyReqMapper.remove(id);
            if (num == 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), "删除变更表数据失败");
            }
            num = baseModifyDetailMapper.remove(id);
        } else {
            num = baseModifyDetailMapper.cellHaveChange(id);
            if (num == 0) {
                return CommonResult.ResponseResult(0, "该数据已经是未修改数据，无需重置");
            }
            num = baseModifyDetailMapper.removeIsNull(id);
        }


        return CommonResult.ResponseResult(num);
    }

    /**
     * 行专列  房屋结构添加房屋
     *
     * @param id
     * @return
     */
    public int changeHouse(String id, String buildId, String cellId) {
        int no = 1;
        List<String> list = modifyDetailMapper.getColumnListByHouse(id);
        List<String> houseIds = modifyDetailMapper.selectByReqNoType(id, null);
        Map<String, Object> map = new HashMap<>(8);
        map.put("list", list);
        map.put("id", id);
        //添加房屋
        for (String houseId : houseIds) {
            map.put("houseId", houseId);
            BaseHouseInfo baseHouseInfo = baseHouseInfoMapper.getChangeColumnListAll(map);
            no = baseHouseInfoMapper.insert(baseHouseInfo);
            if (no == 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), "添加房屋操作失败");
            }
        }
        //删除房屋
        String ids = modifyDetailMapper.selectDelHouse(id, null);
        if (StringUtils.isNotBlank(ids)) {
            no = baseHouseInfoMapper.delHouse(ids);
            if (no == 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), "删除房屋操作失败");
            }
            //删除业主
            int nu = baseOwnerInfoMapper.selectHouseOwner(ids);
            if (nu > 0) {
                no = baseOwnerInfoMapper.delOwners(ids);
                if (no == 0) {
                    throw new CourseException(ShiroResultEnum.FAIL.getCode(), "删除业主操作失败");
                }
            }
        }

        //修改房屋（当初没有维护现在维护的数据）
        List<String> changeHouseId = baseModifyDetailMapper.reqNoChangeHouse(id);
        List<String> changeHouseInfo = modifyDetailMapper.selectBusinessNextByReqNoId(id);
        for (String chang : changeHouseInfo) {
            changeHouseId.add(chang);
        }
        for (String chan : changeHouseId) {
            int changeHouse = baseModifyDetailMapper.selectChangeHouseById(id, chan);
            if (changeHouse == 0) {
                map.put("houseId", chan);
                BaseHouseInfo baseHouseInfo = baseHouseInfoMapper.getChangeColumnListAll(map);
                BaseOwnerInfo baseOwnerInfo = baseOwnerInfoMapper.getInfoByAccNo(baseHouseInfo.getId());
                if (null == baseOwnerInfo) {
                    baseHouseInfo.setState(NumStrEnum.ZERO.getNum());
                } else {
                    baseHouseInfo.setState(NumStrEnum.ONE.getNum());
                }
                no = baseHouseInfoMapper.update(baseHouseInfo);
                if (no == 0) {
                    throw new CourseException(ShiroResultEnum.FAIL.getCode(), "修改房屋操作失败");
                }
            }
        }
        return no;
    }

    /**
     * 行专列
     *
     * @param id
     * @return
     */
    private int change(String id, String buildId) {
        int no = 1;
        List<String> list = modifyDetailMapper.getColumnListByHouse(id);
        List<String> houseIds = modifyDetailMapper.selectByReqNoType(id, null);
        Map<String, Object> map = new HashMap<>(8);
        map.put("list", list);
        map.put("id", id);
        //添加房屋
        for (String houseId : houseIds) {
            map.put("houseId", houseId);
            BaseHouseInfo baseHouseInfo = baseHouseInfoMapper.getChangeColumnList(map);
            no = baseHouseInfoMapper.insert(baseHouseInfo);
            if (no == 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), "添加房屋操作失败");
            }
        }
        //删除房屋
        String ids = modifyDetailMapper.selectDelHouse(id, NumStrEnum.ONE.getNum());
        if (StringUtils.isNotBlank(ids)) {
            no = baseHouseInfoMapper.delHouse(ids);
            if (no == 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), "删除房屋操作失败");
            }
        }
        //删除业主
        int nu = baseOwnerInfoMapper.selectHouseOwner(ids);
        if (nu > 0) {
            no = baseOwnerInfoMapper.delOwners(ids);
            if (no == 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), "删除业主操作失败");
            }
        }
        //修改结构
        List<String> buildCell = baseCellInfoMapper.selectCellByBuild(buildId);
        List<String> changeBuildCell = modifyDetailMapper.selectByReqNoId(id);
        for (int i = 0; i < changeBuildCell.size(); i++) {
            if (buildCell.contains(changeBuildCell.get(i))) {
                BaseCellInfo b = baseCellInfoMapper.getInfo(changeBuildCell.get(i));
                BaseCellInfo baseCellInfo = modifyUtil.getCompleteEntityCell(id, b, changeBuildCell.get(i));
                no = baseCellInfoMapper.update(baseCellInfo);
                if (no == 0) {
                    throw new CourseException(ShiroResultEnum.FAIL.getCode(), "修改单元操作失败");
                }
            }
        }
        //删除结构
        String cellIds = modifyDetailMapper.selectDelHouse(id, NumStrEnum.ZERO.getNum());
        if (StringUtils.isNotBlank(cellIds)) {
            no = baseCellInfoMapper.delCell(cellIds);
            if (no == 0) {
                throw new CourseException(ShiroResultEnum.FAIL.getCode(), "删除单元操作失败");
            }
        }
        List<String> cellIdAdd = modifyDetailMapper.selectByReqNoTypeCell(id, NumStrEnum.ZERO.getNum());
        if (null != cellIdAdd || cellIdAdd.size() > 0) {
            for (String cellId : cellIdAdd) {
                List<String> lists = modifyDetailMapper.getColumnListAndCell(id);
                Map<String, Object> mapCell = new HashMap<>(8);
                mapCell.put("list", lists);
                mapCell.put("id", id);
                mapCell.put("cellId", cellId);
                BaseCellInfo baseCellInfo = baseCellInfoMapper.getChangeColumnCell(mapCell);
                no = baseCellInfoMapper.insert(baseCellInfo);
                if (no == 0) {
                    throw new CourseException(ShiroResultEnum.FAIL.getCode(), "添加单元操作失败");
                }
            }
        }
        return no;
    }
}

